package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import com.jiguang.shangcheng.dao.ShangChengDao;
import com.jiguang.shangcheng.pojo.*;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/shang/shang_selectOrder")
public class selectAllOrder extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        //获取参数
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("name");

        //通过ID查询我购卖的订单
        ShangChengDao shangChengDao = new ShangChengDao();
        List<Order> mybuy = shangChengDao.selectMyBuyOrder(user.getId());
        //查询商品的信息
        List<Product1> mybuy_product = new ArrayList<>();
        for (Order order : mybuy) {
            Product1 product1 = new Product1();
            //通过商品ID查询商品
            AdminDao adminDao = new AdminDao();
            Product product = adminDao.selectProductByid(order.getProductId());
            product1.setId(order.getId());
            product1.setName(product.getName());
            product1.setPrice(product.getPrice());
            product1.setGame(product1.getGame());
            product1.setOrderState(order.getState());
            product1.setPhoto(product.getPhoto());
            //查询商品的商家
            User user1 = adminDao.selectUserByid(product.getUserId());
            product1.setUsername(user1.getNetname());
            mybuy_product.add(product1);
        }

        req.setAttribute("mybuy",mybuy_product);
        req.setAttribute("buy",mybuy_product.size());


        //查询我售卖的订单
        AdminDao adminDao = new AdminDao();
        List<Order> orderList = adminDao.selectAllOrder(1,1000);
        List<Product1> mysole_product = new ArrayList<>();
        for (Order order : orderList) {
            Product1 product1 = new Product1();
            //通过product_id查询商品的信息
            Product product = adminDao.selectProductByid(order.getProductId());
            //通过用户ID查询用户信息
            User user1 = adminDao.selectUserByid(product.getUserId());
            if(user1.getId().equals(user.getId())){
                product1.setId(order.getId());
                product1.setName(product.getName());
                product1.setPrice(product.getPrice());
                product1.setGame(product.getGame());
                product1.setOrderState(order.getState());
                product1.setPhoto(product.getPhoto());
                product1.setUsername(user1.getNetname());
                //加入
                mysole_product.add(product1);
            }
        }

        req.setAttribute("mysole",mysole_product);
        req.setAttribute("sole",mysole_product.size());
        //跳转
        req.getRequestDispatcher("shang_order.jsp").forward(req,resp);
    }
}
